<template>
  <div class="loginContainer">
    <el-form class="loginForm"  ref="loginForm" :model="loginForm"  autocomplete="on">
      <el-form-item >
        <div class="loginTitle">
          系统登录
        </div>
      </el-form-item>
      <el-form-item prop="username">
        <el-input 
        id="username"
        prefix-icon="el-icon-user-solid" 
        v-model="loginForm.username" 
        placeholder="请输入用户名"
        name="username"
        type="text"
        tabindex="1"
        autocomplete="on"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input 
        id="password"
        prefix-icon="el-icon-lock" 
        type="password" 
        v-model="loginForm.password" 
        placeholder="请输入密码"
        :key="passwordType"
        ref="password"
        name="password"
        tabindex="2"
        autocomplete="on"
        @keyup.native="checkCapslock"
        @blur="capsTooltip = false"
        @keyup.enter.native="handleLogin"
        />
      </el-form-item>


    <el-form-item>
      <el-row :gutter="20">
        <el-col :span="16" :offset="0">
          <el-input id="vertify" prefix-icon="el-icon-question" v-model="loginForm.vertify" placeholder="请输入验证码" @keyup.enter.native="handleLogin"></el-input>
        </el-col>
        <el-col :span="8" :offset="0">
           <img class="kaptchaImg" id="kaptchaImg" @click="getKaptchaImage"  src="http://localhost:8081/image/getKaptchaImage">      
        </el-col>
      </el-row>
    </el-form-item>
    

    <el-form-item>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-button class="btn" type="primary" size="default" @click.native.prevent="handleLogin">登录</el-button>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-button class="btn" type="default" size="default">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'esp',
        password: '123456',
        vertify: '11111'
      },
      // loginRules: {
      //   username: [{ required: true, trigger: 'blur', validator: validateUsername }],
      //   password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      // },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    getKaptchaImage(){
       var loginImg = document.getElementById("kaptchaImg");
	  	 loginImg.src = "http://localhost:8081/image/getKaptchaImage?time=" + new Date();
    },
    handleLogin() {
      console.log("1==>开始执行handleLogin方法 ")
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          console.log("2==>input校验通过，开始执行vuex中的login方法")
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            console.log("6==>监听到后端api登录接口成功回调，触发路由前置钩子，开始登录到首页")
            this.$router.push({ path: this.redirect || '/' })
            console.log("13==>页面跳转完成！！登录到首页了:",this.redirect)
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .loginContainer{
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    .loginForm{
      height: 350px;
      width: 400px;
      border-radius: 10px;
      box-shadow: 0 0 25px #cac6c6;
      padding: 20px 35px;
      .loginTitle{
        display: flex;
        justify-content: center;
        align-items: center;
        font-weight: 600;
        font-size: 24px;
      }
      .verify{
        border: 1px solid #dcdfe6;
        cursor: pointer;
      }
      .btn{
        width: 100%;
      }
      .kaptchaImg{
        width: 97px;
        height: 39px;
        cursor: pointer;
      }
      
    }
  }
</style>